//
//  MessageCell.m
//  QQ聊天
//
//  Created by 伍银荣 on 2021/6/5.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "MessageCell.h"
#import "Messages.h"


#define Width [UIScreen mainScreen].bounds.size.width
#define timeFont [UIFont systemFontOfSize:19]
#define textFont [UIFont systemFontOfSize:18]


@interface MessageCell ()
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UIButton *textBtn;

@property (nonatomic, copy) NSString *imgName;

@end

@implementation MessageCell

-(void)setMessage:(Messages *)message {
    _message = message;
    
    //获取控件的数据和frame
    [self setupData];
    [self setupFrame];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //创建控件
        //1.时间time
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = timeFont;
        timeLabel.textAlignment = NSTextAlignmentCenter;
        //timeLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:timeLabel];
        _timeLabel = timeLabel;
        
        //2.icon头像
        UIImageView *iconImgView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconImgView];
        _iconImgView = iconImgView;
        
        //3.正文btn
        UIButton *textBtn = [[UIButton alloc]init];
        [textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //textBtn.backgroundColor = [UIColor blueColor];
        textBtn.titleLabel.numberOfLines = 0;
        textBtn.titleLabel.font = textFont;
        [self.contentView addSubview:textBtn];
        _textBtn = textBtn;
    }
    return self;
}

- (void)setupData {
    _timeLabel.text = _message.time;
    [_textBtn setTitle:_message.text forState:UIControlStateNormal];
    if (_message.type == 0) {
        _iconImgView.image = [UIImage imageNamed:@"2"];
        [_textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    } else {
        _iconImgView.image = [UIImage imageNamed:@"1"];
        [_textBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
}

- (void)setupFrame {
    CGFloat margin = 10;
    //时间
    CGFloat timeW = Width;
    CGFloat timeH = 25;
    CGFloat timeX = 0;
    CGFloat timeY = margin;
    if ([_message.time isEqualToString:@""]) {
        _timeLabel.frame = CGRectMake(0, 0, 0, 0);
    } else {
       _timeLabel.frame = CGRectMake(timeX, timeY, timeW, timeH);
    }
    
    
    //icon
    CGFloat iconW = 50;
    CGFloat iconH = iconW;
    CGFloat iconY = CGRectGetMaxY(_timeLabel.frame) + margin;
    CGFloat iconX;
    if (_message.type == 0) {
        iconX = Width - margin - iconW;
    } else {
        iconX = margin;
    }
    _iconImgView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //正文btn
   CGSize textSize = [_message.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: textFont} context:nil].size;
    CGFloat textW = textSize.width;
    CGFloat textH = textSize.height;
    CGFloat textY = iconY + margin;
    CGFloat textX;
    if (_message.type == 0) {
        textX = Width - 60 - margin - textW;
    } else {
        textX = CGRectGetMaxX(_iconImgView.frame) + margin;
    }
    _textBtn.frame = CGRectMake(textX, textY, textW, textH);
//    _textBtn.center = CGPointMake(textX, iconH * 0.5);
    NSLog(@"x = %f, w = %f , h = %f",textX, textW,textH);
    
    if (_message.text.length > 30) {
        _rowHeight = CGRectGetMaxY(_textBtn.frame) + 10;
    } else {
        _rowHeight = CGRectGetMaxY(_iconImgView.frame) + margin;
    }   
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
